package com.pp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pp.common.pojo.Admin;
import com.pp.mapper.AdminMapper;
import com.pp.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public String login(Admin admin) {
        //1.获取原始密码
        String password = admin.getPassword();
        System.out.println(password);
        //2.将密码进行加密处理
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(md5Str);
        //3.将密文传递给对象
        admin.setPassword(md5Str); //username/password
        System.out.println(admin);
        //根据对象中不为null的属性充当where条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", admin.getUsername());
        //4.根据条件查询数据库
        Admin adminDB = adminMapper.selectOne(queryWrapper);
        System.out.println(adminDB);
        //5.定义token数据 限定条件 token不能重复
        // UUID: 随机字符串 3.4*10^38 种可能性
        String uuid = UUID.randomUUID().toString()
                .replace("-","");
        System.out.println(uuid);
        return adminDB==null?null:uuid;
    }
}
